package ex03.connector.http;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @packgeName: ex03.connector.http
 * @ClassName: HttpConnector
 * @copyright: CopyLeft
 * @description:<描述>
 * @author: lili
 * @date: 2017/8/11-16:15
 * @version: 1.0
 * @since: JDK 1.8
 */
public class HttpConnector implements Runnable {

    boolean stopped;
    private String scheme = "http";

    public String getScheme(){
        return scheme;
    }


    @Override
    public void run() {
        ServerSocket serverSocket = null;
        int port = 8080;
        try{
            serverSocket = new ServerSocket(port,1, InetAddress.getByName("127.0.0.1"));
        }catch (IOException e){
            e.printStackTrace();
            System.exit(1);
        }

        while(!stopped){
            Socket socket = null;
            try {
                serverSocket.accept();
            } catch (IOException e) {
//                e.printStackTrace();
                continue;
            }
            //处理每一个请求
            HttpProcessor processor = new HttpProcessor(this);
            processor.process(socket);
        }
    }

    public void start(){
        Thread thread = new Thread(this);
        thread.start();
    }
}
